package hk.ust.cse.comp3111.studentSchedule;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class RegisteredClassDatabaseHandler extends SQLiteOpenHelper{

	
	// All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;
 
    // Database Name
    private static final String DATABASE_NAME = "course";
 
    // Contacts table name
    private static final String TABLE_REGISTERED_COURSE = "registeredCourse";
 
    // Contacts Table Columns names
    private static final String KEY_CLASSNO = "classNo";
    private static final String KEY_COURSECODE = "courseCode";

    
    
    public RegisteredClassDatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
 
    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_REGISTERED_COURSE + "("
                + KEY_CLASSNO + " INTEGER PRIMARY KEY," 
        		+ KEY_COURSECODE + " TEXT,"           
        		+ ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }
 
    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_REGISTERED_COURSE);
        // Create tables again
        onCreate(db);
    }
    
    public void register(Course course){
    	SQLiteDatabase db = this.getWritableDatabase();
    	ContentValues values = new ContentValues();

    	values.put(KEY_CLASSNO, course.getClassNo());
    	values.put(KEY_COURSECODE, course.getCourseCode());

    	db.insert(TABLE_REGISTERED_COURSE, null, values);
    	db.close();
    }
    
    
    
    public List<String> getAllCourseCode(){
    	List<String> list = new ArrayList<String>();
    	String selectQuery = "SELECT DISTINCT courseCode FROM " + TABLE_REGISTERED_COURSE;
    	
    	SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        
        if (cursor.moveToFirst()) {
            do {
            	list.add(cursor.getString(cursor.getColumnIndex(KEY_COURSECODE)));
            } while (cursor.moveToNext());
        }
        
        return list;

    }
    
    public List<Integer> getAllClassNumber(){
    	List<Integer> list = new ArrayList<Integer>();
    	String selectQuery = "SELECT classNo FROM " + TABLE_REGISTERED_COURSE;
    	
    	SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        
        if (cursor.moveToFirst()) {
            do {
            	list.add(cursor.getInt(cursor.getColumnIndex(KEY_COURSECODE)));
            } while (cursor.moveToNext());
        }
        
        return list;

    }
    
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
